//
//  AppDelegate+Alipay.m
//  支付宝免密Demo
//
//  Created by LXL on 2022/8/17.
//

#import "AppDelegate+Alipay.h"
#import <AFServiceSDK/AFServiceSDK.h>

@implementation AppDelegate (Alipay)

// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options{
    NSString *UrlStr = [url.absoluteString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    if ([url.host isEqualToString:@"apmqpdispatch"]) {///支付宝支付并开通免密
        ///这个具体看调用时的Block回调
        [AFServiceCenter handleResponseURL:url withCompletion:^(AFServiceResponse *response) {
            if (AFResSuccess == response.responseCode) {
                NSLog(@"%@", response.result);
            }
        }];
    }else if ([UrlStr containsString:@"safepay"]) {///支付宝支付
            UrlStr = [UrlStr stringByReplacingOccurrencesOfString:@"alipayDemo://safepay/?" withString:@""];
            NSData *jsonData = [UrlStr dataUsingEncoding:NSUTF8StringEncoding];
            NSError *error;
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
            if (!error) {
                ///发送支付结果的通知
                [[NSNotificationCenter defaultCenter]postNotificationName:@"kAliPayCallBackEvent" object:dic];
            }
    }else if ([UrlStr containsString:@"lixiaolu.evzhuangjia.com"]) {///微信支付
        ///发送微信支付回来的通知，没有成功或失败的结果反馈，需要原生调JS方法刷新
        [[NSNotificationCenter defaultCenter]postNotificationName:@"kWeChatPayCallBackEvent" object:nil];
    }
    return YES;
}

@end
